package com.mo.ld.activity.view

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.github.mikephil.charting.charts.HorizontalBarChart
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.LegendEntry
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.formatter.ValueFormatter
import com.mo.ld.R
import com.mo.ld.base.BBaseListActivity
import com.mo.ld.bean.ActionBean
import com.mo.ld.modle.getMainAdapter2
import com.mo.lib.ui.dialog.BaseDialog
import com.mo.lib.ui.dialog.BaseDialog2
import com.mo.lib.ui.dialog.DismissType
import com.mo.lib.ui.dialog.setDismissType
import com.mo.lib.utils.KLog
import com.mo.lib.view.chart.*
import com.mo.lib.view.recyclerview.layoutG

/**
 * @ author：mo
 * @ data：2022/8/24:14:20
 * @ 功能：图表
 */
class BarchartActivty2 : BBaseListActivity<ActionBean>() {
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        viewBinding.krvBaseList.layoutG(3)
    }

    override fun getList(page: Int) {
    }

    override fun getMore(page: Int) {
    }

    override fun creatAdaper(): BaseQuickAdapter<ActionBean, BaseViewHolder> {
        return getMainAdapter2(mutableListOf(
            ActionBean("水平-对称柱状图") {
               object :BaseDialog(R.layout.dia_chart_bar_h){
                   override fun doWhat() {
                       dialog.findViewById<HorizontalBarChart>(R.id.hbc_dialog).setDescription2() {}.setLegend2 {
                           //            isEnabled = false
                           val legendEntry = LegendEntry() //自定义图例：设置后会覆盖原生图例，且设置后不可追加图例
                           legendEntry.label="应检查"
                           legendEntry.formColor= Color.GREEN
                           val legendEntry2 = LegendEntry() //自定义图例：设置后会覆盖原生图例，且设置后不可追加图例
                           legendEntry2.label="已检查"
                           legendEntry2.formColor= Color.RED
                           setCustom(mutableListOf(legendEntry,legendEntry2 ))
                           horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER //水平方向位置：   LEFT, CENTER, RIGHT    默认LEFT
                           yOffset=20f
                       }.setChartSetting2 {}.setXAxis2 {
                           setTextSize(6f);
                           //            setAxisMaximum(0.6f);
                           //把最小值设置为负数能够为下方留出点空白距离
                           setAxisMinimum(-0.6f);
                           //不显示网格
                           setDrawGridLines(false);
                           setDrawAxisLine(true);
                           //将x轴显示在左侧
                           setPosition(XAxis.XAxisPosition.BOTH_SIDED);
                           setLabelCount(11);
                           //自定义样式
                           setValueFormatter(object : ValueFormatter(){
                               override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                                   return  (value * 10 ).toString() +  "-" + (value + 1) * 10;
                               }
                           });
                       }.setAxisLeft2 {
                           isEnabled = false
                           setAxisMinimum(-25f);
                           setAxisMaximum(25f);
                       }.setAxisRight2 {
                           setTextSize(6f);
                           setAxisMinimum(-25f);
                           setAxisMaximum(25f);
                           setTextColor(Color.BLACK);
                           setDrawGridLines(false);
                           setDrawLabels(true);
                           setValueFormatter(object : ValueFormatter() {
                               override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                                   return Math.abs(value).toString() + " MM";
                               }
                           })
                       }.loadBarData(arrayListOf(BarEntry(0f, floatArrayOf(11f, 11f)), BarEntry(1f, floatArrayOf(10f, 10f)), BarEntry(2f, floatArrayOf(9f, 9f)), BarEntry(3f, floatArrayOf(8f, 8f))), ""
                           , barDataSet = {
                               setColors( resources.getColor(com.mo.lib.R.color.text_green), resources.getColor(com.mo.lib.R.color.text_red))//依次设置每次柱块的颜色
                           }
                           , barData = {
                               barWidth = 0.3f //设置柱块的宽度
                               setValueFormatter(object : ValueFormatter() {
                                   override fun getBarLabel(barEntry: BarEntry?): String {
                                       //                        return String.format("%.2f", barEntry!!.y) + "%" //保留两位小数
                                       return ""
                                   }
                               })
                               setValueTextColor(Color.GREEN)
                               setValueTextSize(6f)
                               setValueTypeface(Typeface.DEFAULT_BOLD)
                               KLog.i(dataSets.size.toString())
                           }
                       )
                   }
               }.setDismissType(DismissType.OTHER).show()
        },
            ActionBean("水平-柱状图") {
                BaseDialog2(R.layout.dia_chart_bar_h){
                    it.findViewById<HorizontalBarChart>(R.id.hbc_dialog).setDescription2() {}.setLegend2 {
                        isEnabled = false
                    }.setChartSetting2 {}.setXAxis2 {
                        textSize = 12f //文本大小
                        position = XAxis.XAxisPosition.BOTTOM //设置X轴所在位置   默认为上面,值：BOTTOM,BOTH_SIDED,BOTTOM_INSIDE,TOP,TOP_INSID
                        labelCount = 4 //X轴标签数量
                        setDrawGridLines(false) //是否绘制X轴上的网格线（背景里面的竖线 HorizontalBarChart里的横线）
                        valueFormatter = object : ValueFormatter() {
                            override fun getAxisLabel(value: Float, axis: AxisBase?): String { //设置标签显示内容
                                return arrayOf("燃气经营", "燃气运输", "燃气用户", "燃气具")[value.toInt()] //注意这里不需要显示的要改成 ""
                            }
                        }
                        axisLineColor = Color.rgb(245, 133, 179); //X轴颜色
                        axisLineWidth = 2f;   //X轴粗细
                    }.setAxisLeft2 {
                        isEnabled = false
                    }.setAxisRight2 {
                        textSize = 12f
                        axisMinimum = 0f
                        setDrawGridLines(false)
                        setValueFormatter(object : ValueFormatter() {
                            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                                return value.toInt().toString()
                            }
                        })
                    }.loadBarData(arrayListOf(BarEntry(0f, 2.51f), BarEntry(1f, 3.51f), BarEntry(2f, 5.51f), BarEntry(3f, 7.51f)), "") {
                        barWidth = 0.3f //设置柱块的宽度
                        setValueFormatter(object : ValueFormatter() {
                            override fun getBarLabel(barEntry: BarEntry?): String {
                                return String.format("%.2f", barEntry!!.y) + "%" //保留两位小数
                            }
                        })
                        setValueTextColor(Color.GREEN)
                        setValueTextSize(12f)
                        setValueTypeface(Typeface.DEFAULT_BOLD)
                        (dataSets[0] as BarDataSet).setColors(resources.getColor(com.mo.lib.R.color.blue),
                            resources.getColor(com.mo.lib.R.color.text_red),
                            resources.getColor(com.mo.lib.R.color.text_green),
                            resources.getColor(com.mo.lib.R.color.text_red)) //依次设置每次柱块的颜色
                    }
                }.setDismissType(DismissType.OTHER).show()
            },
            ActionBean("水平-重叠柱状图"){
                BaseDialog2(R.layout.dia_chart_bar_h){
                    it.findViewById<HorizontalBarChart>(R.id.hbc_dialog).setDescription2() {}.setLegend2 {
                        //            isEnabled = false
                        val legendEntry = LegendEntry() //自定义图例：设置后会覆盖原生图例，且设置后不可追加图例
                        legendEntry.label="应检查"
                        legendEntry.formColor=Color.GREEN
                        val legendEntry2 = LegendEntry() //自定义图例：设置后会覆盖原生图例，且设置后不可追加图例
                        legendEntry2.label="已检查"
                        legendEntry2.formColor=Color.RED
                        setCustom(mutableListOf(legendEntry,legendEntry2 ))
                        horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER //水平方向位置：   LEFT, CENTER, RIGHT    默认LEFT
                        stackSpace=-50f
                    }.setChartSetting2 {}.setXAxis2 {
                        textSize = 12f //文本大小
                        position = XAxis.XAxisPosition.BOTTOM //设置X轴所在位置   默认为上面,值：BOTTOM,BOTH_SIDED,BOTTOM_INSIDE,TOP,TOP_INSID
                        labelCount = 4 //X轴标签数量
                        setDrawGridLines(false) //是否绘制X轴上的网格线（背景里面的竖线 HorizontalBarChart里的横线）
                        valueFormatter = object : ValueFormatter() {
                            override fun getAxisLabel(value: Float, axis: AxisBase?): String { //设置标签显示内容
                                return arrayOf("燃气具", "燃气用户", "燃气运输", "燃气经营")[value.toInt()] //注意这里不需要显示的要改成 ""
                            }
                        }
                        axisLineColor = Color.rgb(245, 133, 179); //X轴颜色
                        axisLineWidth = 2f;   //X轴粗细
                    }.setAxisLeft2 {
                        isEnabled = false
                    }.setAxisRight2 {
                        textSize = 12f
                        axisMinimum = 0f
                        setDrawGridLines(false)
                        setValueFormatter(object : ValueFormatter() {
                            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                                return value.toInt().toString()
                            }
                        })
                    }.loadBarData(arrayListOf(BarEntry(0f, floatArrayOf(11f, 11f)), BarEntry(1f, floatArrayOf(10f, 10f)), BarEntry(2f, floatArrayOf(9f, 9f)), BarEntry(3f, floatArrayOf(8f, 8f))), ""
                        , barDataSet = {
                            setColors( resources.getColor(com.mo.lib.R.color.text_green), resources.getColor(com.mo.lib.R.color.text_red))//依次设置每次柱块的颜色
                        }
                        , barData = {
                            barWidth = 0.3f //设置柱块的宽度
                            setValueFormatter(object : ValueFormatter() {
                                override fun getBarLabel(barEntry: BarEntry?): String {
                                    //                        return String.format("%.2f", barEntry!!.y) + "%" //保留两位小数
                                    return ""
                                }
                            })
                            setValueTextColor(Color.GREEN)
                            setValueTextSize(12f)
                            setValueTypeface(Typeface.DEFAULT_BOLD)
                            KLog.i(dataSets.size.toString())
                        }
                    )
                }.setDismissType(DismissType.OTHER).show()
            },
            ActionBean("水平-柱状组"){
                BaseDialog2(R.layout.dia_chart_bar_h){
                    it.findViewById<HorizontalBarChart>(R.id.hbc_dialog).setDescription2() {}.setLegend2 {
                        isEnabled = false
                    }.setChartSetting2 {}.setXAxis2 {
                        textSize = 12f //文本大小
                        position = XAxis.XAxisPosition.BOTTOM //设置X轴所在位置   默认为上面,值：BOTTOM,BOTH_SIDED,BOTTOM_INSIDE,TOP,TOP_INSID
//                        labelCount = 2 //X轴标签数量
                        setDrawGridLines(false) //是否绘制X轴上的网格线（背景里面的竖线 HorizontalBarChart里的横线）
                        valueFormatter = object : ValueFormatter() {
                            override fun getAxisLabel(value: Float, axis: AxisBase?): String { //设置标签显示内容
                                return arrayOf("一般隐患", "重大隐患")[value.toInt()] //注意这里不需要显示的要改成 ""
                            }
                        }



                        axisLineColor = Color.rgb(245, 133, 179); //X轴颜色
                        axisLineWidth = 2f;   //X轴粗细
                        setAxisMaximum(2f)
                        setAxisMinimum(0f)
                        setLabelCount(2, false)
                    }.setAxisLeft2 {
                        isEnabled = false
                    }.setAxisRight2 {
                        textSize = 12f
                        axisMinimum = 0f
                        setDrawGridLines(false)
                        setValueFormatter(object : ValueFormatter() {
                            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                                return value.toInt().toString()
                            }
                        })


                    }.loadBarDataGroup(mutableListOf<List<Float>>(mutableListOf(10f,20f), mutableListOf(30f,40f))
                        ,  mutableListOf<Int>(com.mo.lib.R.color.text_red,com.mo.lib.R.color.text_green)
                    ) {
//                        barWidth = 0.3f //设置柱块的宽度
                        setValueFormatter(object : ValueFormatter() {
                            override fun getBarLabel(barEntry: BarEntry?): String {
                                return String.format("%.2f", barEntry!!.y) + "%" //保留两位小数
                            }
                        })
                        setValueTextColor(Color.GREEN)
                        setValueTextSize(12f)
                        setValueTypeface(Typeface.DEFAULT_BOLD)
//                        (dataSets[0] as BarDataSet).setColors(
////                            resources.getColor(com.mo.lib.R.color.blue),
////                            resources.getColor(com.mo.lib.R.color.black),
//                            resources.getColor(com.mo.lib.R.color.text_red),
//                            resources.getColor(com.mo.lib.R.color.text_green),
////                            resources.getColor(com.mo.lib.R.color.text_red)
//                                                ) //依次设置每次柱块的颜色
                    }
                }.setDismissType(DismissType.OTHER).show()
            },
            ActionBean("水平-柱状组2"){
                val xAxisValues= mutableListOf<Float>(0f,1f)
                val labels= mutableListOf<String>("","")
                val colours= mutableListOf<Int>(Color.parseColor("#123456"),Color.parseColor("#987654"))
                val yAxisValues= mutableListOf<List<Float>>(mutableListOf(10f,20f), mutableListOf(30f,40f))
                BaseDialog2(R.layout.dia_chart_bar_h){
                    val mm=BarChartManager(   it.findViewById<HorizontalBarChart>(R.id.hbc_dialog))
                    mm.showMoreBarChart(xAxisValues, yAxisValues, labels, colours)
                    mm.setXAxis(2f, 0f, 2)
                }.setDismissType(DismissType.OTHER).show()
            }

        ))
    }


}